package com.baizhi.controller;

import com.baizhi.entity.Book;
import com.baizhi.entity.Category;
import com.baizhi.service.BookService;
import com.baizhi.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

@Controller
@RequestMapping("back/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("show")
    public String showBook(@RequestParam(defaultValue = "1")Integer pageNum,
                           @RequestParam(defaultValue = "5")Integer pageSize,
                           Model model,
                           @RequestParam(defaultValue = "")String query,
                           @RequestParam(defaultValue = "")String value){
        PageInfo<Book> bookPageInfo = bookService.showPageBooks(pageNum, pageSize,query,value);
        model.addAttribute("pageInfo",bookPageInfo);
        model.addAttribute("query",query);
        model.addAttribute("value",value);
        return "forward:/back/book/show.jsp";
    }
    @RequestMapping("showOne")
    public String showOne(Model model,Integer id){
       model.addAttribute("categories",categoryService.select());
        Book book = bookService.showOne(id);
        model.addAttribute("book",book);
        return "forward:/back/book/update.jsp";
    }
    @RequestMapping("add")
    public String add(MultipartFile pic,Book book, HttpServletRequest request)throws IOException {
        String path=request.getServletContext().getRealPath("/images/book/");
        InputStream in=pic.getInputStream();
        OutputStream out=new FileOutputStream(path+pic.getOriginalFilename());
        FileCopyUtils.copy(in,out);
        book.setCover(pic.getOriginalFilename());
        book.setCreateDate(new Date());
        book.setSale(0);
        bookService.addBook(book);
        return "redirect:/back/book/show.do";
    }
    @RequestMapping("update")
    public String updateBook(Book book,MultipartFile pic,HttpServletRequest request)throws IOException{
        if(pic!=null && !pic.isEmpty()){
                String path=request.getServletContext().getRealPath("/images/book/");
                InputStream in = pic.getInputStream();
                OutputStream out=new FileOutputStream(path+pic.getOriginalFilename());
                FileCopyUtils.copy(in,out);
                book.setCover(pic.getOriginalFilename());
                book.setCreateDate(new Date());
                book.setSale(0);
        }
        bookService.updateBook(book);
        return "redirect:/back/book/show.do";
    }
    @RequestMapping("remove")
    public String removeBook(HttpSession session, Integer id, Integer pageNum, Integer pageSize){
        try {
            bookService.removeBook(id);
        }catch (Exception e){
            session.setAttribute("msg",e.getMessage());
        }
        return "redirect:/back/book/show.do?pageNum="+pageNum+"&pageSize="+pageSize+"";
    }
}
